/**   
 * @Title: LoginTask.java 
 * @Package: com.rename.icode.task 
 * @Description: Administrator版权所有
 * @author Administrator  
 * @date 2015-2-12 下午2:57:01 
 * @version  
 */

package com.rename.icode.task;

import android.graphics.drawable.AnimationDrawable;
import android.net.ParseException;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.rename.icode.R;
import com.rename.icode.activity.ActivityFragmentSupport;
import com.rename.icode.activity.ActivitySupport;
import com.rename.icode.activity.ICodeApplication;
import com.rename.icode.activity.LoginActivity;
import com.rename.icode.activity.MainActivity;
import com.rename.icode.activity.SignUpActivity;
import com.rename.icode.comm.Constants;
import com.rename.icode.manager.AppConfigManager;
import com.rename.icode.manager.UserManager;
import com.rename.icode.model.User;
import com.rename.icode.utils.FileUtil;
import com.rename.icode.utils.HttpURLTools;
import com.rename.icode.utils.LogUtil;
import com.rename.icode.utils.StringUtil;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description 实现了...功能
 * @author Administrator
 * @date 2015-2-12 下午2:57:01
 * @version V1.0
 */
// public class LoginTask extends AsyncTask<Params, Progress, Result>
public class LoginTask extends AsyncTask<String, Integer, Integer> {

	private static final String TAG = "LoginTask";

	private User mUser = null;
	private AnimationDrawable loginAnim = null;
	private ActivitySupport activity;
	private ActivityFragmentSupport activityFragment;

	private boolean loginInloginActivity = false;

	/**
	 * Creates a new instance of LoginTask. Description
	 * 
	 * @param loginActivity
	 * @param background
	 */

	public LoginTask(ActivitySupport activity,
			AnimationDrawable animationDrawable) {
		this.activity = activity;
		this.loginAnim = animationDrawable;
		this.loginInloginActivity = true;
	}
	
	public LoginTask(ActivityFragmentSupport activityFragment, AnimationDrawable animationDrawable){
		this.activityFragment = activityFragment;
		this.loginAnim = animationDrawable;
		this.loginInloginActivity = false;
	}

	/**
	 * Description
	 * 
	 * @param params
	 * @return
	 * @see android.os.AsyncTask#doInBackground(Params[])
	 */

	@Override
	protected Integer doInBackground(String... params) {
		Integer result = login();

		Log.i("wxm", "result is = " + result);

		return result;
	}

	@Override
	protected void onPreExecute() {
		// start login animation.
		if (mUser == null) {
			loginAnim.start();
		}
		super.onPreExecute();
	}

	@Override
	protected void onPostExecute(Integer result) {
		loginAnim.stop();
		switch (result) {
		case Constants.SERVER_SUCCESS: // login success.
			AppConfigManager.getInstance().saveLoginConfig();// save login info
			if(loginInloginActivity){
				activity.setResult(activity.RESULT_OK);
				activity.finish();
			}else{
				((MainActivity)activityFragment).changeLayout(MainActivity.MAIN); // 切换到主界面
			}
			
			LogUtil.i(TAG, "Constant.SERVER_SUCCESS");
			break;
		case Constants.SERVER_FAIL:
			LogUtil.i(TAG, "SERVER_FAIL");
			loginAnim.setVisible(false, false);
			if(loginInloginActivity){
				Toast.makeText(activity, activity.getString(R.string.login_fail), 0).show();
				activity.resetLoginBtn();
				activity.hideLoginAnim();
			}else{
				Toast.makeText(activityFragment, activityFragment.getString(R.string.login_fail), 0).show();
				((MainActivity)activityFragment).setLoginBtnSignBtnVisible();
				((MainActivity)activityFragment).hideLoginAnim();
			}
			
			break;
		}
		super.onPostExecute(result);
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		super.onProgressUpdate(values);
	}

	private Integer login() {
		String username = AppConfigManager.getInstance().getUsername();
		String password = AppConfigManager.getInstance().getPassword();
		String result = null;
		
		Map<String, String> params = new HashMap<String, String>();
		params.put("userinfo", username);
		params.put("password", password);
		HttpResponse response = HttpURLTools.sendHttpPost(Constants.LOGIN_URL,
				params);
		if (response != null) {
			try {
				result = EntityUtils.toString(response.getEntity());
				LogUtil.i(TAG, "login up result is " + result);
				if ((Constants.SERVER_FAIL + "").equals(result)) {
					return Constants.SERVER_FAIL;
				}
			} catch (ParseException | IOException e) {
				e.printStackTrace();
			}
		} else {
			LogUtil.i(TAG, "response is null");
			return Constants.SERVER_FAIL;
		}

		// 保存登录信息
		AppConfigManager.getInstance().setPassword(password);
		AppConfigManager.getInstance().setUsername(username);

		UserManager.getInstance().init(result);// 初始化用户数据
		return Constants.SERVER_SUCCESS;
		
	}

}
